<?php
declare (strict_types=1);

namespace magein\sms\service\think;

use magein\sms\SmsException;
use magein\utils\Result;
use think\queue\Job;

class SmsJob
{
    const NAME = 'sms';

    public function fire(Job $job, $data)
    {
        if ($job->attempts() > 3) {
            //通过这个方法可以检查这个任务已经重试了几次了
            $job->delete();
            return true;
        }

        $delay = $data['delay'] ?? 0;

        try {
            /**
             * @var Result $result
             */
            $result = app()->sms->send->message($data);
            if (!$result->getCode()) {
                $job->delete();
            } else {
                $delay && $job->release($delay);
            }
        } catch (SmsException $smsException) {
            // 重新发布这个任务
            $delay && $job->release($delay);
        }
    }

    public function failed($data)
    {

    }
}